Page({
	data: {
		urlAddress: getApp().globalData.imageUrl,
		groupList: [],
		note: "参团",
		shopName: '',
		iconUrl: getApp().globalData.imageUrl + "/pinTuan_an.png",
		downIconUrl: getApp().globalData.imageUrl + "/pingtuan_en.png",
		flag: true,
		message: '以下小伙伴正在发起拼团,您可以直接参加'
	},
	onShow: function() {
		this.getAllGroup();
		this.getLowPrice();
		this.getShopName();
	},
	onHide: function() {
		clearInterval(this.timer)
	},
	onLoad: function(options) {
		// 生命周期函数--监听页面加载
		this.getBanner(3);
	},
	getShopName: function() {
		var _self = this;
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/shop/getShop.do',
			data: {
				shopCode: getApp().globalData.common.reserveShopCode,
			},
			header: getApp().globalData.header,
			success: function(res) {
				if (res.data.code == 200) {
					getApp().globalData.common.longitude = res.data.obj.longitude;
					getApp().globalData.common.latitude = res.data.obj.latitudes;

					_self.setData({
						shopName: res.data.obj.shopName
					});
				} else if (res.data.code == 507) {
					_self.setData({
						shopName: '门店歇业'
					});
				} else {
					_self.setData({
						shopName: '没有该门店'
					});
				}
			}
		})
	},
	getLowPrice: function() {
		var _self = this;
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/group/getCheapPrice.do',
			data: {
				shopCode: getApp().globalData.common.reserveShopCode,
			},
			header: getApp().globalData.header,
			success: function(res) {
				console.log('***getLowPrice**')
				console.log(res)
				_self.setData({
					note: res.data.obj + '元起'
				})
			}
		})
	},
	getAllGroup: function() {

		var _self = this;
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/group/getGroups.do',
			data: {
				shopCode: getApp().globalData.common.reserveShopCode,
			},
			header: getApp().globalData.header,
			success: function(res) {
				console.log(res)
				if (res.data.code == 200) {
					for (var i = 0; i < res.data.obj.length; i++) {
						if (res.data.obj[i].commanderUrl == '' || !res.data.obj[i].commanderUrl) {
							res.data.obj[i].commanderUrl = 'http://static.24xiaomai.com/go/wxapplet/1.3.0/image/default_avatar.png'
						}
					}
					_self.setData({
						groupList: res.data.obj

					})
					_self.getShop();
				} else {
					_self.setData({
						groupList: null
					})
				}
			}
		})

	},
	getBanner: function(type) {
		var _self = this;
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/banner/getBanner.do?bannerType=' + type,
			data: {
				shopCode: getApp().globalData.common.reserveShopCode,
			},
			header: getApp().globalData.header,
			success: function(res) {
				console.log(res)
				if (res.data.code == 200) {

					_self.setData({
						topUrl: res.data.obj
					})


				} else {

				}
			}
		})

	},

	getShop: function() {
		var _self = this;
		wx.showLoading({
			title: '加载中',
			mask: true
		})
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/group/getGroupGoods.do',
			data: {
				shopCode: getApp().globalData.common.reserveShopCode
			},
			header: getApp().globalData.header,
			success: function(res) {
				wx.hideLoading()
				if (res.data.code == 200) {
					for (var i = 0; i < _self.data.groupList.length; i++) {
						_self.data.groupList[i].goodsList = res.data.obj;
						_self.data.groupList[i].isHidden = true;
					}
					_self.setData({
						groupList: _self.data.groupList
					})
					console.log("_self.data.groupList")
					console.log(_self.data.groupList)
				} else {
					_self.setData({
						isShowShop: false,
						noteMsg: '商家歇业',
						shopInfo: {
							shopName: '请选择门店'
						}

					})

				}
			}
		})

	},
	showGoods: function(e) {
		var code = e.currentTarget.dataset.code;
		for (var i = 0; i < this.data.groupList.length; i++) {
			if (code == this.data.groupList[i].groupId) {
				this.data.groupList[i].isHidden = !this.data.groupList[i].isHidden;
			} else {
				this.data.groupList[i].isHidden = true;
			}
		}
		this.setData({
			groupList: this.data.groupList,

		})
	},

	turnAction: function(e) {
		var code = e.currentTarget.dataset.code;
		var gcode = e.currentTarget.dataset.gcode;
		var gid = e.currentTarget.dataset.gid;
		wx.navigateTo({
			url: '../pintuanPay/pintuanPay?code=' + code + '&gcode=' + gcode + '&gid=' + gid
		})
	},
	turnDetail: function(e) {
		var code = e.currentTarget.dataset.code;
		wx.navigateTo({
			url: '../groupDetail/groupDetail?orderCode=' + code
		})
	},
	turnSecondPage: function(e) {
		var path = e.currentTarget.dataset.code;
		if (!path) {
			return
		}
		if (path.indexOf('http') > -1) {
			wx.navigateTo({
				url: '/pages/test/test?path=' + path
			})
			return
		}
		if (path) {
			wx.navigateTo({
				url: path
			})
		} else {
			console.log('没有')
		}
	}

})